use bevy::prelude::*;

use crate::game::planting::{
    PlantingUiState,
    component::{
        BackpackIndexComponent, BackpackMoveFastListComponent, FastListIndexComponent,
        FastListMoveBackpackComponent, GoodsComponent,
    },
    event::*,
    resource::*,
};

// 检查鼠标点击
pub fn handle_click(
    In(block_map_pos): In<Result<IVec2, String>>,
    mouse_button_input: Res<ButtonInput<MouseButton>>,
    mut commands: Commands,
) {
    if mouse_button_input.just_pressed(MouseButton::Left) {
        if let Ok(pos) = block_map_pos {
            commands.trigger(EMouseLeftClick { pos });
        }
    }
}

pub fn test_btn_click(mut commands: Commands, keyboard_input: Res<ButtonInput<KeyCode>>) {
    //TODO 临时测试用 时间加速 加快一天
    if keyboard_input.just_released(KeyCode::KeyQ) {
        commands.trigger(ENextDay);
    }
    //TODO 临时测试用  保存游戏
    if keyboard_input.just_released(KeyCode::KeyP) {
        commands.trigger(ESaveGame);
    }
}

pub fn input_fast_select(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut fast_list: ResMut<FastList>,
) {
    if keyboard_input.just_released(KeyCode::ArrowUp) {
        fast_list.select_up();
    }
    if keyboard_input.just_released(KeyCode::ArrowDown) {
        fast_list.select_down();
    }
}

pub fn input_backpack_select(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut backpack: ResMut<MasterBackpack>,
) {
    if keyboard_input.just_released(KeyCode::ArrowUp) {
        backpack.select_up();
    }
    if keyboard_input.just_released(KeyCode::ArrowDown) {
        backpack.select_down();
    }
}

pub fn input_ui_state_change(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    curr_state: Res<State<PlantingUiState>>,
    mut state: ResMut<NextState<PlantingUiState>>,
) {
    if keyboard_input.just_released(KeyCode::KeyV) {
        if *curr_state.get() != PlantingUiState::FastList {
            state.set(PlantingUiState::FastList);
        }
    }
    if keyboard_input.just_released(KeyCode::KeyB) {
        if *curr_state.get() != PlantingUiState::Backpack {
            state.set(PlantingUiState::Backpack);
        }
    }
}

pub fn input_backpack_keycode(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    backpack: Res<MasterBackpack>,
    fast_lits_index_query: Query<(Entity, &FastListIndexComponent), Without<GoodsComponent>>,
    backpack_index_query: Query<(Entity, &BackpackIndexComponent), With<GoodsComponent>>, //获取物品信息
    mut commands: Commands,
) {
    if keyboard_input.just_released(KeyCode::Space) {
        if fast_lits_index_query.iter().len() > 0 {
            let index = backpack.select_index + backpack.offset;
            if let Some((entity, _)) = backpack_index_query
                .iter()
                .find(|(_, index_cpt)| index == index_cpt.index)
            {
                commands
                    .entity(entity)
                    .insert(BackpackMoveFastListComponent);
            }
        } else {
            info!("快捷栏已满");
        }
    }
}

pub fn input_fast_list_keycode(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    fast_list: Res<FastList>,
    fast_lits_index_query: Query<(Entity, &FastListIndexComponent), With<GoodsComponent>>,
    backpack_query: Query<(Entity, &BackpackIndexComponent), Without<GoodsComponent>>,
    mut commands: Commands,
) {
    if keyboard_input.just_released(KeyCode::Space) {
        if backpack_query.iter().len() > 0 {
            let index = fast_list.select_index;
            if let Some((entity, _)) = fast_lits_index_query
                .iter()
                .find(|(_, index_cpt)| index == index_cpt.index)
            {
                commands
                    .entity(entity)
                    .insert(FastListMoveBackpackComponent);
            }
        } else {
            info!("背包已满");
        }
    }
}
